#python modules
import logging
import os
import datetime

#web modules
import webapp2
import jinja2

#google modules
from google.appengine.api import users

#application modules
from config import config
from models.models import *

jinja_environment = jinja2.Environment(autoescape=True,loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), '../views')))

class GridHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        system_date = datetime.datetime.now().strftime("%d-%m-%Y")
        
        monthService = MonthService()
        months = monthService.get_all(user)

        ###FIRST LOGIN WORK-AROUND###
        if len(months) == 0:
            budgetService = BudgetService()
            budgetService.initialize(config.year,user)
            months = monthService.get_all(user)
        

        # We are using the template module to output the page.
        template = jinja_environment.get_template('month_grid.html')
        template_values = {'username':user.nickname(),
                           'url': users.create_logout_url("/"),
                           'system_date':system_date,
                           'months':months}
        self.response.out.write(template.render(template_values))


class EditHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        system_date = datetime.datetime.now().strftime("%d-%m-%Y")
        
        monthService = MonthService()

        id_month = int(self.request.GET['id_month'])
        month = monthService.get_by_id(id_month,user)

        # We are using the template module to output the page.
        template = jinja_environment.get_template('month_edit.html')		
        template_values = {'username':user.nickname(),
                           'url': users.create_logout_url("/"),
                           'system_date':system_date,
                           'month':month}
        self.response.out.write(template.render(template_values))

    def post(self):
        logging.info("getting user...")
        user = users.get_current_user()
        
        monthService = MonthService()

        logging.info("getting action...")
        is_save = self.request.get('btnSave')
        is_cancel = self.request.get('btnCancel')
        id_month = self.request.get('id_month')

        logging.info("Save: %s, Cancel: %s, Id_cmonth: %s" % (is_save,is_cancel,id_month))
        logging.info(self.request.arguments())

        if is_cancel:
            pass
        else:
            logging.info ("Saving Month....")
            month = monthService.get_by_id(int(id_month),user)
                
            month.income = float(self.request.get('income'))
            month.extra_income = float(self.request.get('extra_income'))
            monthService.save(month,user)

        logging.info ("Returning....")
        self.redirect("/month/grid/")
